//
//  LoginViewController.m
//  muscleLife
//
//  Created by xalo on 16/4/28.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameText;
@property (weak, nonatomic) IBOutlet UITextField *passWordText;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //编辑的时候显示小叉号
    self.userNameText.clearButtonMode = UITextFieldViewModeWhileEditing;
    //密码输入再次编辑的时候就清空
    self.passWordText.clearsOnBeginEditing = YES;
    self.passWordText.secureTextEntry = YES;
    //return键变成什么键
    self.passWordText.returnKeyType = UIReturnKeyGo;
    self.userNameText.returnKeyType = UIReturnKeyNext;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginAction:(UIButton *)sender {

    MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在登陆...";
    [AVUser logInWithUsernameInBackground:self.userNameText.text password:self.passWordText.text block:^(AVUser *user, NSError *error) {

        if (user != nil){

            [hud hide:YES];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [hud hide:YES];
            NSString* message;
            switch (error.code) {
                case 6:
                    message = @"网络连接错误!";
                    break;
                case 211:
                    message = @"用户名或密码错误";
                    break;
                default:
                    message = @"登陆失败";
                    break;
            }
            [self showAlertwithmessage:message];
            NSLog(@"登录失败%@",error);
        }
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    //取消第一响应者
    [textField resignFirstResponder];

    //如果是用户名的页面，则跳转到密码输入框
    if (textField == self.userNameText) {
        
        [self.passWordText becomeFirstResponder];
        return YES;
    }
    if (self.userNameText.text&&self.passWordText.text) {
        [self loginAction:nil];
    }
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //触摸屏幕释放键盘
    [self.view endEditing:YES];
}

-(void)showAlertwithmessage:(NSString*)message{

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
